home *** CD-ROM | disk | FTP | other *** search
/ Nebula 2 / Nebula Two.iso / SourceCode / Crossword / Source / CrosswordSquare.m < prev    next >
Text File  |  1995-06-12  |  4KB  |  179 lines

  1. /*
  2.  
  3. File CrosswordSquare.m
  4.  
  5. Crossword squares are the elements of a crossword matrix.  Each square is either transparent, in which case it appears as black, or contains a letter.  A square with a letter may be drawn using any color combination.  A square can also display a clue number in its upper left hand corner.
  6.  
  7. */
  8.  
  9. #import <appkit/appkit.h>
  10. #import <dpsclient/dpsclient.h>
  11.  
  12. #import "CrosswordSquare.h"
  13. #import "CrosswordSquarePS.h"
  14. #import "Crossword.h"
  15.  
  16.  
  17. /* ————————————————————————————————————————————————————————————————————————————  */
  18.  
  19.  
  20. #define VERSION            1
  21.  
  22.  
  23. /* ————————————————————————————————————————————————————————————————————————————  */
  24.  
  25.  
  26. @implementation CrosswordSquare
  27.  
  28. - (BOOL) isOpaque            {    return opaque;        }
  29. - (char) getLetter            {    return letter;        }
  30. - (int) getNumber            {    return number;        }
  31. - (squareColor) getColor    {    return color;        }
  32. - (id) getData                {    return data;        }
  33.  
  34. - setLetter: (char) theLetter        {    letter = theLetter;        return self;    }
  35. - setNumber: (int) theNumber        {    number = theNumber;        return self;    }
  36. - setColor: (squareColor) theColor    {    color = theColor;        return self;    }
  37. - setData: (id) theData                {    data = theData;            return self;    }
  38.  
  39.  
  40. /* ————————————————————————————————————————————————————————————————————————————  */
  41.  
  42.  
  43. /*
  44.  
  45. There are currently two different versions of crossword squares that need to be unarchived.
  46.  
  47. */
  48.  
  49. + initialize
  50. {
  51.     [CrosswordSquare  setVersion: VERSION];
  52.     
  53.     return self;
  54. }
  55.  
  56.  
  57. - init
  58. {
  59.     [super  init];
  60.     
  61.     opaque = YES;
  62.     color.background = NXConvertGrayToColor(1.0);
  63.     color.text = NXConvertGrayToColor(0.0);
  64.     letter = number = EMPTY;
  65.     
  66.     return self;
  67. }
  68.  
  69.  
  70. - write: (NXTypedStream *) stream
  71. {
  72.     [super  write: stream];
  73.     
  74.     NXWriteTypes(stream, "cci", &opaque, &letter, &number);
  75.     NXWriteColor(stream, color.background);
  76.     NXWriteColor(stream, color.text);
  77.     
  78.     return self;
  79. }
  80.  
  81.  
  82. - read: (NXTypedStream *) stream
  83. {
  84.     [super  read: stream];
  85.     switch (NXTypedStreamClassVersion(stream, "CrosswordSquare"))
  86.     {
  87.         case 1:
  88.             NXReadTypes(stream, "cci", &opaque, &letter, &number);
  89.             color.background = NXReadColor(stream);
  90.             color.text = NXReadColor(stream);
  91.             break;
  92.         
  93.         default:
  94.             break;
  95.     }
  96.     
  97.     return self;
  98. }
  99.  
  100.  
  101. - readOld: (NXTypedStream *) stream
  102. {
  103.     [super  read: stream];
  104.     NXReadTypes(stream, "cc", &opaque, &letter);
  105.     color.background = NXReadColor(stream);
  106.     color.text = NXReadColor(stream);
  107.     
  108.     return self;
  109. }
  110.  
  111.  
  112. /* ————————————————————————————————————————————————————————————————————————————  */
  113.  
  114.  
  115. /*
  116.  
  117. Here are the routines that concern drawing and tracking the mouse.  The user can fill and erase squares symmetrically across the puzzle by holding down one of the modifier keys.  Squares are drawn using two colors: one for the background, one for the text.
  118.  
  119. */
  120.  
  121. - (BOOL) startTrackingAt: (NXPoint *) point  inView: (id) matrix
  122. {
  123.     int        r, c;
  124.     int        h, w;
  125.     
  126.     [self  paint: matrix];
  127.     if ([matrix  mouseDownFlags] & (NX_ALTERNATEMASK | NX_SHIFTMASK))
  128.     {
  129.         [matrix  getRow: &r  andCol: &c  ofCell: self];
  130.         [matrix  getNumRows: &h  numCols: &w];
  131.         [[matrix  cellAt: (h - r - 1): (w - c - 1)]  paint: matrix];
  132.     }
  133.     
  134.     return NO;
  135. }
  136.  
  137.  
  138. - paint: (id) matrix
  139. {
  140.     if (opaque != [matrix  getFill])
  141.     {
  142.         opaque = !opaque;
  143.         [matrix  drawCell: self];
  144.     }
  145.     
  146.     return self;
  147. }
  148.  
  149.  
  150. - drawInside: (const NXRect *) frame  inView: (id) matrix
  151. {
  152.     id        font;
  153.  
  154.     if (opaque)
  155.     {
  156.         [matrix  lockFocus];
  157.         [font = [matrix  font]  set];
  158.         
  159.         NXSetColor(color.background);
  160.         NXRectFill(frame);
  161.         NXSetColor(color.text);
  162.         
  163.         PSWdrawSquare(
  164.     
  165.                 letter,
  166.                 number,
  167.                 frame->origin.x,
  168.                 frame->origin.y,
  169.                 frame->size.width,
  170.                 [font  metrics]->capHeight * [font  pointSize] );
  171.         
  172.         [matrix  unlockFocus];
  173.     }
  174.     
  175.     return NO;
  176. }
  177.  
  178.  
  179. @end